package com.asking.service;

import com.asking.dao.UserMapper;
import com.asking.domain.User;
import com.asking.utils.JWTUtil;
import com.asking.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class LoginService {
    @Resource
    private UserMapper userMapper;


    public Result login(User user) {
        try {
            List<User> select = userMapper.select(user);
            if (select.size() == 0){
                return new Result(404,"手机号或密码错误");
            }else if(select.size() > 1){
                //TODO 测试数据
                return new Result(500,"服务器异常");
            }else{
                String sign = JWTUtil.sign(user.getPhone(), user.getPassword());
                return new Result(200,"登陆成功",sign);
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO 测试数据
            return  new Result(500,"服务器异常");
        }

    }
}
